const request = require("request.js").request;
const app = getApp();

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}


//时间格式化
 function formatDate(timeStamp) {
  //return new Date(parseInt(timeStamp) * 1000).toLocaleString();
  let time = new Date(timeStamp);
  let year = time.getFullYear();
  let month = (time.getMonth() + 1);
  let day = (time.getDate());
  let hour = (time.getHours());
  let mimute = (time.getMinutes());
  let second = (time.getSeconds());
  return {
    year, month, day, hour, mimute, second
  }
}

//收藏活动点
function collectRequest(params, alter) {
  request('m/activity/interest.json', params).then(function (res) {
    console.log(res)
    if (res.data.success) {
      if(typeof res.data.result != "undefined") {
        wx.showToast({
          title: res.data.message,
          icon: '',
          image: '../../images/warn.png',
          duration: 500,
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else {
        if (res.data.isInterest) {

          wx.showToast({
            title: '收藏成功',
            icon: '',
            image: '',
            duration: 500,
            mask: true,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          wx.showToast({
            title: '取消收藏',
            icon: '',
            image: '',
            duration: 500,
            mask: true,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    
    } else {
      wx.showToast({
        title: res.data.message,
        icon: '',
        image: '../../images/warn.png',
        duration: 500,
        mask: true,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }

    alter(res.data.isInterest, res.data.count);
    
  });
}

//刷新收藏状态
function freshState(activityId,list,that){
  if (typeof (app.globalData.flag) == "number") {
    for(let i = 0; i < list.length; i ++) {
      if (list[i].activityId == activityId) {
        console.log('aaa', list[i]);
        let param = {};
        let key = "list[" + i + "].interest";
        let value = !list[i].interest;
        param[key] = value;
        that.setData(param);
        console.log('bbb', list[i]);        
      }
    }
  }
}

//获取用户ID
function getUserId(callbBack) {
  wx.login({
    success: function (res) {
        const appid = 'wx6b9118fc1cf14616'; //填写微信小程序appid  
        const secret = '62457457b874e283945b2314e97f8c4b'; //填写微信小程序secret  
      wx.request({
        url: 'https://api.weixin.qq.com/sns/jscode2session',
        data: {
          "appid": appid,
          "secret": secret,
          "js_code": res.code,
          "grant_type": "authorization_code"
        },
        header: {
          'content-type': 'application/json'
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          callbBack(res.data.openid);
        },
        fail: function (res) {
          callbBack(null);
        },
        complete: function (res) { },
      })
    },
    fail: function (res) { },
    complete: function (res) { },
  })
}

//判断用户是否登录
function isLogin(callback){
  if (app.globalData.phone == "") {
    wx.navigateTo({
      url: '../login/login',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    });
    return;
  } else{
    callback();
  }
}







module.exports = {
  formatTime: formatTime,
  collectRequest: collectRequest,
  freshState: freshState,
  isLogin: isLogin,
  formatDate: formatDate
}
